Skip to content

Method: validateLexicalFormField(Field, boolean)

1: /**
2: * Copyright (C) 2020 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.model.metamodel;
16:
17: import cz.cvut.kbss.jopa.exception.InvalidFieldMappingException;
18: import cz.cvut.kbss.jopa.model.annotations.OWLAnnotationProperty;
19: import cz.cvut.kbss.jopa.model.annotations.OWLDataProperty;
20: import cz.cvut.kbss.jopa.utils.IdentifierTransformer;
21:
22: import java.lang.reflect.Field;
23: import java.lang.reflect.ParameterizedType;
24: import java.lang.reflect.Type;
25: import java.util.List;
26: import java.util.Map;
27: import java.util.Set;
28: import java.util.SortedSet;
29:
30: import static cz.cvut.kbss.jopa.utils.Constants.SUPPORTED_IDENTIFIER_TYPES;
31:
32: /**
33: * Verifies that a field's mapping metadata and declaration are valid.
34: */
35: class FieldMappingValidator {
36:
37: void validatePropertiesField(Field field) {
38: assert field != null;
39: if (!Map.class.isAssignableFrom(field.getType())) {
40: throw new InvalidFieldMappingException(
41: "Expected @Properties field to be a map, but it is a " + field.getType());
42: }
43: if (isRawType(field.getGenericType())) {
44: throw new InvalidFieldMappingException("@Properties field cannot be a raw map.");
45: }
46: final PropertiesParametersResolver parametersResolver = new PropertiesParametersResolver(field);
47: if (!isValidIdentifierType(parametersResolver.getKeyType())) {
48: throw new InvalidFieldMappingException(
49: "@Properties key type is not a valid identifier type. Expected one of " +
50: SUPPORTED_IDENTIFIER_TYPES);
51: }
52: validatePropertiesValueType(parametersResolver.getValueType());
53: }
54:
55: private static boolean isRawType(Type type) {
56: return !(type instanceof ParameterizedType);
57: }
58:
59: private static void validatePropertiesValueType(Type type) {
60: if (isRawType(type)) {
61: throw new InvalidFieldMappingException(
62: "Expected a java.util.Set as value parameter of the @Properties map, but got " + type);
63: }
64: if (!((ParameterizedType) type).getRawType().equals(Set.class)) {
65: throw new InvalidFieldMappingException(
66: "Expected a java.util.Set as value parameter of the @Properties map, but got " + type);
67: }
68: }
69:
70: void validateTypesField(Field field) {
71: if (!Set.class.isAssignableFrom(field.getType())) {
72: throw new InvalidFieldMappingException("Expected @Types field to be a set, but it is a " + field.getType());
73: }
74: if (isRawType(field.getGenericType())) {
75: throw new InvalidFieldMappingException("@Types field cannot be a raw set.");
76: }
77: final ParameterizedType typeSpec = (ParameterizedType) field.getGenericType();
78: if (!isValidIdentifierType(typeSpec.getActualTypeArguments()[0])) {
79: throw new InvalidFieldMappingException(
80: "@Types field value is not a valid identifier type. Expected one of " + SUPPORTED_IDENTIFIER_TYPES);
81: }
82: }
83:
84: void validateIdentifierType(Type type) {
85: if (!isValidIdentifierType(type)) {
86: throw new InvalidFieldMappingException(type + " is not a valid identifier type.");
87: }
88: }
89:
90: boolean isValidIdentifierType(Type type) {
91: return type instanceof Class && IdentifierTransformer.isValidIdentifierType((Class<?>) type);
92: }
93:
94: void validateAnnotationPropertyField(Field field, OWLAnnotationProperty config) {
95: assert field != null;
96: assert config != null;
97: validateLexicalFormField(field, config.lexicalForm());
98: validateSimpleLiteralField(field, config.simpleLiteral());
99: }
100:
101: void validateDataPropertyField(Field field, OWLDataProperty config) {
102: assert field != null;
103: assert config != null;
104: validateLexicalFormField(field, config.lexicalForm());
105: validateSimpleLiteralField(field, config.simpleLiteral());
106: }
107:
108: private static void validateLexicalFormField(Field field, boolean lexicalForm) {
109:• if (lexicalForm && !String.class.isAssignableFrom(getLiteralFieldType(field))) {
110: throw new InvalidFieldMappingException("lexicalForm mapping can be used only on fields of type String.");
111: }
112: }
113:
114: private static void validateSimpleLiteralField(Field field, boolean simpleLiteral) {
115: if (simpleLiteral && !String.class.isAssignableFrom(getLiteralFieldType(field))) {
116: throw new InvalidFieldMappingException("simpleLiteral mapping can be used only on fields of type String.");
117: }
118: }
119:
120: private static Class<?> getLiteralFieldType(Field field) {
121: final Class<?> fieldType = field.getType();
122: if (List.class.isAssignableFrom(fieldType) || Set.class.isAssignableFrom(fieldType) ||
123: SortedSet.class.isAssignableFrom(fieldType)) {
124: final ParameterizedType typeSpec = (ParameterizedType) field.getGenericType();
125: assert typeSpec.getActualTypeArguments().length == 1;
126: return (Class<?>) typeSpec.getActualTypeArguments()[0];
127: }
128: return fieldType;
129: }
130: }